﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace CloudStorageLight.Core
{

    /// <summary>
    /// ユーザ情報
    /// </summary>
    [DataContract]
    public class BlobUser
    {

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string OperatorName { get; set; }

        [DataMember]
        private List<string> Roles { get; set; }

        [DataMember]
        public bool IsAdmin { get; set; }

        public bool IsGuest { get; set; }

        private IPrincipal principal;

        public BlobUser(string name):this(new GenericPrincipal(new GenericIdentity(name),null))
        {
            this.IsAdmin = false;
            this.IsGuest = true;
        }

        public BlobUser(IPrincipal principal)
        {
            this.principal = principal;
            Roles = new List<string>();

            if (principal != null && principal.Identity.IsAuthenticated)
            {
                this.Name = principal.Identity.Name;
            }
            else
            {
                this.Name = "GUEST";
                this.IsGuest = true;
            }

        }

        public virtual bool IsInRole(string role)
        {
            role = role.ToLower();
            if (principal != null && principal.Identity.IsAuthenticated)
            {
                if (role == Name.ToLower()) return true;
                return principal.IsInRole(role);
            }
            return false;
        }
    }
}
